<?php
class ModelTicketMessage extends Model {
	public function addMessage($data) {
		if (!isset($data['file'])){
			$data['file'] = '';
		}
		
      	$this->db->query("INSERT INTO " . DB_PREFIX . "ticket_message SET content = '" . $this->db->escape(nl2br($data['message'])) . "', ticket_id = '" . (int)$data['ticket_id'] . "', is_user = 1, created = NOW(), file = '" . (string)$data['file'] . "'");
	}
	
	public function getMessages($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "ticket_message` WHERE ";
		
		if ( isset($data['filter_ticket']) && $data['filter_ticket'] != 0 ){
			$sql .= " ticket_id = " . (int)$data['filter_ticket'];
		}
			
		$sort_data = array(
			'created'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY created";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
}
?>